package cn.px.rest.controller.lubricationmanage;

import cn.px.common.util.R;
import cn.px.dao.dto.lubricationmanage.LubricationPlanStandardRelationDTO;
import cn.px.dao.entity.lubricationmanage.LubricationPlanStandardRelation;
import cn.px.service.lubricationmanage.LubricationPlanStandardRelationService;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.lubricationmanage.ExportLubricationPlanStandardRelationVO;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.rest.log.annotation.SysLog;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 润滑计划标准关联 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/lubricationmanage/lubrication-plan-standard-relation")
public class LubricationPlanStandardRelationController {

  private final  LubricationPlanStandardRelationService lubricationPlanStandardRelationService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<LubricationPlanStandardRelation> getById(@PathVariable("id") String id){
      return new R<>(lubricationPlanStandardRelationService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
   * @return R
   */
  @SysLog("新增润滑计划标准关联")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO) {
      return new R<>(lubricationPlanStandardRelationService.saveModelByDTO(lubricationPlanStandardRelationDTO));
  }

  /**
   * 修改记录
   *
   * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
   * @return R
   */
  @SysLog("修改润滑计划标准关联")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO) {
      lubricationPlanStandardRelationService.modModelByDTO(lubricationPlanStandardRelationDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除润滑计划标准关联")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-del')")
  public R removeById(@PathVariable String id){
    lubricationPlanStandardRelationService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除润滑计划标准关联")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-del')")
    public R removeIds(@RequestBody List<String> ids){
        lubricationPlanStandardRelationService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<LubricationPlanStandardRelation>> query(LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(lubricationPlanStandardRelationService.selectPageInfo(lubricationPlanStandardRelationDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
   * @return R
   */
  @GetMapping("")
  public R query(LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO) {
      return new R<>(lubricationPlanStandardRelationService.getModelListByDTO(lubricationPlanStandardRelationDTO));
  }

  /**
     * 润滑计划标准关联导出
     *
     * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("润滑计划标准关联导出")
  @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-export')")
  public void export(HttpServletResponse response, LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO) {
    List<LubricationPlanStandardRelation> list = lubricationPlanStandardRelationService.getModelListByDTO(lubricationPlanStandardRelationDTO);
    ExcelUtil.exportResponseDict(response, ExportLubricationPlanStandardRelationVO.class, BeanConverterUtil.copyListProperties(list, ExportLubricationPlanStandardRelationVO.class), "润滑计划标准关联");
  }

  /**
   * 关联润滑标准
   *
   * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
   * @return R
   */
  @SysLog("关联润滑计划标准")
  @PostMapping("relate")
  @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-add')")
  public R relate(@Validated({AddGroup.class}) @RequestBody LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO) {
    lubricationPlanStandardRelationService.relate(lubricationPlanStandardRelationDTO);
    return new R<>();
  }

  /**
   * 批量取消润滑计划标准关联
   *
   * @param lubricationPlanStandardRelationDTO 润滑计划标准关联DTO
   * @return R
   */
  @SysLog("批量取消润滑计划标准关联")
  @DeleteMapping("batchCancelRelate")
  @PreAuthorize("@pms.hasPermission('lubricationmanage-lubrication-plan-standard-relation-del')")
  public R removeIds(@RequestBody LubricationPlanStandardRelationDTO lubricationPlanStandardRelationDTO){
    lubricationPlanStandardRelationService.batchCancelRelate(lubricationPlanStandardRelationDTO);
    return new R<>();
  }

}
